package mspbots.cw.business.core.tickets;

import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Jay.Yang
 */
public interface TicketsRepository extends R2dbcRepository<Tickets, Long> {

    /**
     * find entity other id and tenant id
     *
     * @param ticketId   ticketId
     * @param tenantCode tenantCode
     * @return account entity
     */
    Mono<Tickets> findByTicketIdAndTenantCode(Long ticketId, Long tenantCode);

    @Query("select * from task_cw_ticket where tenant_code=:tenantCode and ticket_id=:ticketId and updated_time < :updatedTime")
    Mono<Tickets> findByTicketIdAndTenantCodeAndUpdatedTime(Long ticketId, Long tenantCode, LocalDateTime updatedTime);

    @Query("select count(*) from task_cw_ticket where tenant_code=:tenantCode and note_resolution_flag=true and ticket_id in (:ticketIds)")
    Mono<Integer> findByTicketIdAndTenantCodeOwner(Long tenantCode, List<Long> ticketIds);

}
